const app = getApp();
const db = wx.cloud.database();
Page({
    data: {        
        id: app.globalData.userInfo._id,
        adminName: app.globalData.userInfo.nickName,
        activeName: '',
        questions:[],
        questionsR:[],
        questionsTitle: '',
        selects:[],
        selectsR:[],
        questionIndex: '',
        popShow: false
    },
    onLoad: function (options) {
        db.collection('Management_user').where({
            nickName: this.data.adminName
          }).get().then(res => {
            if(res.data.length === 1){
                this.setData({
                    questions: res.data[0].questions,
                    selects: res.data[0].selects,
                    questionsR: res.data[0].questions,
                    selectsR: res.data[0].selects,
                })
            }
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },
    onChange(event) {
        console.log(event.detail,this.data.questionIndex)
        this.setData({
          activeName: event.detail,
          questionIndex: event.detail,
          questionsTitle: event.detail === '' ?'' : this.data.questions[event.detail]
        });
      },
    onQuestionChange(event) {
        this.data.questions[this.data.questionIndex] = event.detail
    },
    popShow(event) {
        this.setData({
            popShow: true
        })  
        if(this.data.questionIndex === ''){
            this.data.questions.push('')
            this.setData({
                questionIndex: this.data.questions.length -1,
                activeName: this.data.questions.length - 1
            })
            this.data.selects.push([])
        }      
    },
    onSelectChange(event) {
        var index = event.currentTarget.dataset.id
        this.data.selects[this.data.questionIndex][index] = event.detail
    },
    addItem(event) {        
        var obj1 = this.data.selects
        obj1[this.data.questionIndex].push('')
        this.setData({
            selects: obj1
        })
    },
    deleteItem(event) {
        var index = event.currentTarget.dataset.id
        this.data.selects[this.data.questionIndex].splice(index,1)
        this.setData({
            selects: this.data.selects
        })
    },
    onClose(event) {
        this.setData({
            questions: this.data.questionsR,
            selects: this.data.selectsR
        })
        this.data.questions.forEach((value,key) => {
            if(value === ''){
                this.data.questions.splice(key,1)
            }            
        });
        this.onLoad();
    },
    onConfirm(event) {
        db.collection('Management_user').doc(this.data.id).update({
            data: {
                questions: this.data.questions,
                selects: this.data.selects
              },
              success: function(res) {   
                if(res.stats.updated === 1){
                    wx.showToast({
                        title: '修改成功',
                      })
                }else{
                    wx.showToast({
                      title: '修改失败',
                    })
                }
              }
        })
        this.setData({
            questionsR: this.data.questions,
            selectsR: this.data.selects
        })
        this.onLoad();
    }
})